﻿#Region "Copyright"
'————————————————————————————————————————
'	创建人员(Creator)    ：Huangyd
'	创建日期(Date)       ：2013-03-14
'   联系方式(Email)      ：aj-ayumi@163.com; gajayumi@gmail.com; ajayumi@me.com
'   描    述(Description)：
'
'	Copyright (C) 2012-2013 ajayumi 版权所有
'————————————————————————————————————————
#End Region

Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Xml.Serialization

Namespace Xml.Serialization

    ''' <summary>
    ''' 序列化类
    ''' </summary>
    ''' <remarks></remarks>
    Public Class Serializer


        ''' <summary>
        ''' 将对象序列化成二进制流
        ''' </summary>
        ''' <param name="request">待序列化的对象</param>
        ''' <returns>返回序列化后的二进制流</returns>
        Public Shared Function SerializeBinary(ByVal request As Object) As MemoryStream
            Dim serializer As New BinaryFormatter()
            Dim memStream As New System.IO.MemoryStream()
            serializer.Serialize(memStream, request)
            Return memStream
        End Function

        ''' <summary>
        ''' 从二进制流中将对象反序列化
        ''' </summary>
        ''' <param name="data"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function DeSerializeFromBinary(ByVal data() As Byte) As Object
            Using ms As New MemoryStream(data)
                ms.Seek(0, SeekOrigin.Begin)
                Dim deserializer As New BinaryFormatter()
                Dim newobj As Object = deserializer.Deserialize(ms)
                Return newobj
            End Using
        End Function

        ''' <summary>
        ''' 将对象序列化为xml的内存流
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="model"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function SerializeXml(Of T)(ByVal model As T) As MemoryStream
            Dim _XmlSerializer As New System.Xml.Serialization.XmlSerializer(GetType(T))
            Dim memStream As New System.IO.MemoryStream()
            _XmlSerializer.Serialize(memStream, model)
            Return memStream
        End Function

        ''' <summary>
        ''' 从xml中将对象反序列化
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="data"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function DeSerializeFromXml(Of T)(ByVal data() As Byte) As T
            Using ms As New MemoryStream(data)
                ms.Seek(0, SeekOrigin.Begin)
                Dim _XmlSerializer As New System.Xml.Serialization.XmlSerializer(GetType(T))
                Dim _Obj As T = DirectCast(_XmlSerializer.Deserialize(ms), T)
                Return _Obj
            End Using
        End Function

        ''' <summary>
        ''' 将序列化对象保存到xml文件中
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="obj"></param>
        ''' <param name="path"></param>
        ''' <remarks></remarks>
        Public Shared Sub SaveToXml(Of T)(obj As T, path As String)
            Using writer As New StreamWriter(path)
                Dim _XmlSerializer As New XmlSerializer(GetType(T))
                _XmlSerializer.Serialize(writer, obj)
            End Using
        End Sub

        ''' <summary>
        ''' 将序列化对象保存到xml文件中
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="obj"></param>
        ''' <param name="path"></param>
        ''' <remarks></remarks>
        Public Shared Sub SaveToXml(Of T)(obj As T, types() As Type, path As String)
            Using writer As New StreamWriter(path)
                Dim _XmlSerializer As New XmlSerializer(GetType(T), types)
                _XmlSerializer.Serialize(writer, obj)
            End Using
        End Sub


        ''' <summary>
        ''' 从xml文件中加载序列化对象
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="path"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function LoadFromXml(Of T)(path As String) As T
            Dim obj As T = Nothing

            If Not File.Exists(path) Then
                Return obj
            End If

            Using reader As New StreamReader(path)
                Dim _XmlSerializer As New XmlSerializer(GetType(T))
                obj = DirectCast(_XmlSerializer.Deserialize(reader), T)
            End Using

            Return obj
        End Function

        ''' <summary>
        ''' 从xml文件中加载序列化对象
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="path"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function LoadFromXml(Of T)(types() As Type, path As String) As T
            Dim obj As T = Nothing

            If Not File.Exists(path) Then
                Return obj
            End If

            Using reader As New StreamReader(path)
                Dim _XmlSerializer As New XmlSerializer(GetType(T), types)
                obj = DirectCast(_XmlSerializer.Deserialize(reader), T)
            End Using

            Return obj
        End Function




    End Class

End Namespace

